package com.example.mytestdemo.linearlayoutmanager

import android.annotation.SuppressLint
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mytestdemo.BaseViewBindActivity
import com.example.mytestdemo.R
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRecycleviewLayoutBinding
import com.example.mytestdemo.databinding.ActivityLinearinglayoutManagerRotationLayoutBinding
import com.example.mytestdemo.databinding.ActivityRecycleviewItemLayoutBinding
import com.example.mytestdemo.utils.ScreenUtils
import com.example.mytestdemo.viewpage2.adapter.ImyAdapterListener

/**
 *参考：https://blog.csdn.net/u011387817/article/details/80313184
 * https://blog.csdn.net/AliEnCheng/article/details/122541863
 */
class CustomRecycleViewActivity :
    BaseViewBindActivity<ActivityLinearinglayoutManagerRecycleviewLayoutBinding>() {
    lateinit var toast: Toast

    private var mArcSlidingHelper: ArcSlidingHelper? = null
    private lateinit var mView: View

    override fun getLayoutInflateId(): Int {
        return R.layout.activity_linearinglayout_manager_recycleview_layout
    }

    override fun initData() {
    }

    override fun initUI() {
        mView = mBind.recycleView
        toast = Toast.makeText(this, "aa", Toast.LENGTH_SHORT)
        mView.post {
            mArcSlidingHelper = ArcSlidingHelper().create(mView, object : OnSlidingListener {
                override fun onSliding(angle: Float) {
                    mView.setRotation(mView.getRotation() + angle)
                }

            })
        }
        getWindow().getDecorView().setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent): Boolean {
                //处理滑动事件
                mArcSlidingHelper?.handleMovement(event)
                return true
            }

        })

        initAdapter()
    }

   private fun initAdapter() {
        val mData = arrayListOf<String>()
        (0..20).forEach { mData.add("事项$it") }
        val adapter = MyAdapter(mData)
//        mBind.recycleView.layoutManager = LinearLayoutManager(this)
        mBind.recycleView.layoutManager = MaxLineLinearLayoutManager(this,5)
        mBind.recycleView.adapter = adapter
    }

}

class MyAdapter(val mData: List<String>) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {


    private lateinit var mBind: ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        mBind = DataBindingUtil.inflate(
            LayoutInflater.from(parent.context),
            R.layout.activity_linearinglayout_manager_recycleview_item_layout,
            parent,
            false
        )
        return ViewHolder(mBind)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindData(mData[position], position)
    }

    override fun getItemCount(): Int {
        return mData.size
    }


    class ViewHolder(val mBind: ActivityLinearinglayoutManagerRecycleviewItemLayoutBinding) :
        RecyclerView.ViewHolder(mBind.root) {
        fun bindData(data: String, position: Int) {
            mBind.tv.text = data
        }
    }
}